package com.hjm.project.merchantModel.controller;

import com.hjm.common.utils.ExcelUtil;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.framework.web.page.TableDataInfo;
import com.hjm.project.merchantModel.domain.MerchantModel;
import com.hjm.project.merchantModel.service.IMerchantModelService;
import com.hjm.project.merchantModelDetail.domain.MerchantModelDetail;
import com.hjm.project.merchantModelDetail.domain.MerchantModelDetailVo;
import com.hjm.project.merchantModelDetail.service.IMerchantModelDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商户模板费率Controller
 * 
 * @author surplus
 * @date 2022-07-10
 */
@Controller
@RequestMapping("/merchant/merchantModel")
public class MerchantModelController extends BaseController {
    private String prefix = "merchantModel";

    @Resource
    private IMerchantModelService merchantModelService;

    @Resource
    private IMerchantModelDetailService merchantModelDetailService;

    @RequiresPermissions("merchant:MerchantModel:view")
    @GetMapping()
    public String MerchantModel() {
        return prefix + "/merchantModel";
    }

    /**
     * 查询商户模板费率列表
     */
    @RequiresPermissions("merchant:MerchantModel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MerchantModel merchantModel) {
        startPage();
        List<MerchantModel> list = merchantModelService.selectMerchantModelList(merchantModel);
        return getDataTable(list);
    }

    /**
     * 根据代理商政策获取政策相应模版
     */
    @RequiresPermissions("merchant:MerchantModel:list")
    @PostMapping("/modelAgentList")
    @ResponseBody
    public TableDataInfo modelAgentList(MerchantModel merchantModel) {
        startPage();
        List<MerchantModel> list = merchantModelService.selectMerchantModelList(merchantModel);
        return getDataTable(list);
    }


    /**
     * 新增商户模板费率
     */
    @GetMapping("/add")
    public String add(ModelMap mmp) {
     /*   ProductInfo pi = new ProductInfo();
        pi.setState(0);
        List<ProductInfo> productInfos = productInfoService.selectProductInfoList(pi);
        mmp.put("proList",productInfos);*/
        return prefix + "/add";
    }

    /**
     * 新增保存商户模板费率
     */
    @RequiresPermissions("merchant:MerchantModel:add")
    @Log(title = "商户模板费率", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MerchantModel merchantModel) {
        return toAjax(merchantModelService.insertMerchantModel(merchantModel));
    }

    /**
     * 修改商户模板费率
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        MerchantModel merchantModel = merchantModelService.selectMerchantModelById(id);
        mmap.put("merchantModel", merchantModel);
     /*   ProductInfo pi = new ProductInfo();
        pi.setState(0);
        List<ProductInfo> productInfos = productInfoService.selectProductInfoList(pi);
        mmap.put("proList",productInfos);*/
        return prefix + "/edit";
    }

    /**
     * 修改保存商户模板费率
     */
    @RequiresPermissions("merchant:MerchantModel:edit")
    @Log(title = "商户模板费率", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MerchantModel merchantModel) {
        return toAjax(merchantModelService.updateMerchantModel(merchantModel));
    }

    /**
     * 删除商户模板费率
     */
    @RequiresPermissions("merchant:MerchantModel:remove")
    @Log(title = "商户模板费率", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String modelNo) {
        return toAjax(merchantModelService.deleteMerchantModelByNo(modelNo));
    }

    /**
     * 编辑商户费率模板页面
     */
    @GetMapping("/setModelDetailsRate")
    public String setModelDetailsRate(String modelNo,String type, ModelMap mmap) {
        MerchantModelDetail modelDetails = new MerchantModelDetail();
        modelDetails.setModelNo(modelNo);
        List<MerchantModelDetail> merchantModelDetail = merchantModelDetailService.selectMerchantModelDetailList(modelDetails);
        MerchantModelDetailVo modelDetailVo = new MerchantModelDetailVo().dbFormatModelDetailsView(merchantModelDetail);
        modelDetailVo.setModelNo(modelNo);
        mmap.put("mv", modelDetailVo);
        mmap.put("type", type == null ? "0" : "1");
        return prefix + "/setModelDetailsRate";
    }

    /**
     * 编辑费率模板附加费页面
     */
    @GetMapping("/addFj")
    public String addFj(String modelNo, ModelMap mmap) {
        MerchantModelDetail modelDetails = new MerchantModelDetail();
        modelDetails.setModelNo(modelNo);
        MerchantModelDetail merchantModelDetail = merchantModelDetailService.selectMerchantModelDetailOne(modelDetails);
        mmap.put("mmd", merchantModelDetail);
        mmap.put("modelNo", modelNo);
        return prefix + "/addFj";
    }


}
